package com.admin.service.impl;

import com.admin.entity.Station;
import com.admin.entity.TGroup;
import com.admin.mapper.GroupMapper;
import com.admin.requestBody.GroupRequest;
import com.admin.service.IGroupService;
import com.admin.service.IStationService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author marisheng
 * @since 2019-11-21
 */
@SuppressWarnings("all")
@Service
public class GroupServiceImpl extends ServiceImpl<GroupMapper, TGroup> implements IGroupService {

    @Autowired
    private GroupMapper groupMapper;

    @Autowired
    private IStationService iStationService;

    /**
     * 添加分组
     * @param group
     * @return
     */
    @Override
    public int addGroupS(TGroup group) {

        if (group !=null){
            return groupMapper.insert(group);
        }

        return 0;
    }

    /**
     * 用来下拉框选择框 站点的分组
     * @param stationId
     * @return
     */
    @Override
    public List<TGroup> stationGroup(Integer stationId) {
        QueryWrapper q=new QueryWrapper();
        if(stationId!=null && stationId!=0) {
            q.eq("group_station_id", stationId);
        }
        List list = groupMapper.selectList(q);

        return list;
    }

    /**
     * 查询分组
     * @param page
     * @param limit
     * @return
     */
    @Override
    public IPage queryGroupAll(int page, int limit, Map map) {

        Page page1=new Page(page,limit);

        IPage iPage = groupMapper.queryGroupAll(page1,map);

        return iPage;
    }

    @Override
    public List queryGroupAllNotLimit() {

        List<TGroup> tGroups = groupMapper.selectList(null);
        return tGroups;
    }

    /**
     * 修改分组
     * @param tGroup
     * @return
     */
    @Override
    public int updateByIdGroupS(TGroup tGroup) {
        int i = groupMapper.updateById(tGroup);
        return i;
    }

    /**
     * 删除分组
     * @param id
     * @return
     */
    @Override
    public boolean removeByIdGroupS(int id) {
        TGroup tGroup = groupMapper.selectById(id);
        if(tGroup!=null){
            Station station = iStationService.queryByIdStationId(tGroup.getGroupStationId());
            if(station==null){
                return true;
            }
        }
        return false;
    }

    /**
     * 终端列表 的分组 使用
     * @param group
     * @return
     */
   @Override
   public IPage terminalGroupQiYong(int page, int limit, GroupRequest group){

       Page page1=new Page(page,limit);

       QueryWrapper q=new QueryWrapper();

        q.eq("group_station_id",group.getGroupStationId());
        q.eq("group_status","启用");
        if (group.getGroupName()!=null&&group.getGroupName()!=""){
            q.like("group_name",group.getGroupName());

        }

       IPage iPage = groupMapper.selectPage(page1,q);

       return iPage;

    }

}
